// pages/userInf/userInf.js
const app = getApp()
import utils from "../../utils/util"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navBarData: app.globalData.navBarData,
        userinf: {
            username: "一个帅气的名字",
            rating: "0",
        },
        wxuserinf: {},
        hasuserInf: app.globalData.hasLogin,
        dialogShow: false,
        dialogButton: [{
            type: 'primary',
            className: '',
            text: '确定',
            value: 0
        }],
        emptyValue: ""
    },


    loginAndGetProfile() {
        let that = this
        wx.getUserProfile({
            desc: '获取头像',
            success: (res) => {
                let wxInf = res.userInfo
                wx.setStorageSync('wxUserInf', wxInf)
                utils.login().then(function (res) {
                    that.setData({
                        wxuserinf: wxInf,
                        hasuserInf: app.globalData.hasLogin,
                        userinf: app.globalData.userInfo
                    })
                    console.log("缓存过期重新获取")
                    that.getrank()
                }).catch(function (res) {})
            }
        })
    },
    getrank() {
        let that = this
        console.log("拉取用户信息")
        wx.showLoading({
            title: '正在获取排名',
            mask: true
        })
        utils.request({
            url: utils.APIS.getProfile.url,
            method: utils.APIS.getProfile.method,
            data: ""
        }).then(function (ress) {
            wx.hideLoading()
            app.globalData.userInfo = ress.data.info
            that.setData({
                userinf: app.globalData.userInfo
            })
        }).catch(function (ress) {
            wx.hideLoading()
            wx.showToast({
                title: '无法获取排名',
            })
        }).finally(function (e) {

        })
    },

    openRegisteredContest: function (e) {
        if (!app.globalData.hasLogin) {
            wx.showToast({
                title: '请先登录',
                icon: "error"
            })
            return
        }
        app.globalData.contestPageType = 'Registered'
        wx.showLoading({
            title: '加载中',
        })
        wx.hideTabBar({
            animation: false,
        })
        wx.switchTab({
            url: '/pages/exam/exam',
        })
    },


    openSetNameDialog(e) {
        if (this.data.hasuserInf) {
            this.setData({
                dialogShow: true
            })
        }
    },

    dialogTap(e) {
        if (e.detail.index == 0) {
            console.log("修改昵称")
            this.setName()
        }
    },
    inputBind(e) {
        this.setData({
            newName: e.detail.value
        })
    },
    setName() {
        let that = this
        let newname = this.data.newName
        if (newname) {
            wx.showLoading({
                title: '修改中',
            })
            utils.request({
                url: "/api/user/profile",
                method: "POST",
                data: {
                    newdata: {
                        username: newname
                    }
                }
            }).then(function (res) {
                wx.hideLoading()
                if (res.data.ret != 0) {
                    wx.showToast({
                        title: res.data.msg,
                    })
                } else {
                    wx.showToast({
                        title: '修改成功',
                    })
                    that.setData({
                        dialogShow: false
                    })
                    app.globalData.userInfo.username = newname
                    that.setData({
                        userinf: app.globalData.userInfo
                    })
                }
            }).catch(function (e) {
                wx.hideLoading()
            }).finally(function (e) {

                that.setData({
                    emptyValue: ""
                })
            })
        } else {
            wx.showToast({
                title: '请输入新的昵称',
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        utils.initGlobalData(this)

        let that = this
        let wxInf = wx.getStorageSync("wxUserInf")
        if (wxInf) {
            utils.login().then(function (res) {
                that.setData({
                    wxuserinf: wxInf,
                    hasuserInf: app.globalData.hasLogin,
                    userinf: app.globalData.userInfo
                })
                console.log("缓存信息")
                that.getrank()
            }).catch(function (res) {})

        } else {
            this.loginAndGetProfile()
        }

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        utils.resetGlobalData()
        this.setData({
            userinf: app.globalData.userInfo
        })
        wx.showTabBar({
            animation: false,
        }).catch(function () {})
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.loginAndGetProfile()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})